home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / C++ / DirectInput / MultiMapper / multidi.h < prev    next >
Encoding:
C/C++ Source or Header  |  2004-09-27  |  7.1 KB  |  149 lines

  1. //-----------------------------------------------------------------------------
  2. // File: MultiDI.h
  3. //
  4. // Desc: Multiple user DirectInput support using action mapping
  5. //
  6. // Copyright (C) Microsoft Corporation. All Rights Reserved.
  7. //-----------------------------------------------------------------------------
  8. #ifndef MULTIDI_H
  9. #define MULTIDI_H
  10.  
  11. #ifndef DIRECTINPUT_VERSION
  12. #define DIRECTINPUT_VERSION 0x0800
  13. #endif
  14.  
  15. #include <dinput.h>
  16.  
  17. //-----------------------------------------------------------------------------
  18. // Miscellaneous helper functions
  19. //-----------------------------------------------------------------------------
  20. #define SAFE_DELETE(p)       { if(p) { delete (p);     (p)=NULL; } }
  21. #define SAFE_DELETE_ARRAY(p) { if(p) { delete[] (p);   (p)=NULL; } }
  22. #define SAFE_RELEASE(p)      { if(p) { (p)->Release(); (p)=NULL; } }
  23.  
  24. HRESULT DXUtil_WriteStringRegKey( HKEY hKey, LPCTSTR strRegName, LPCTSTR strValue );
  25. HRESULT DXUtil_ReadStringRegKeyCch( HKEY hKey, LPCTSTR strRegName, TCHAR* strDest, DWORD cchDest, LPCTSTR strDefault );
  26. HRESULT DXUtil_ConvertGUIDToStringCch( const GUID* pGuidSrc, TCHAR* strDest, int cchDestChar );
  27. HRESULT DXUtil_ConvertWideStringToGenericCch( TCHAR* tstrDestination, const WCHAR* wstrSource, int cchDestChar );
  28.  
  29. //-----------------------------------------------------------------------------
  30. // Name: DXUtil_Timer()
  31. // Desc: Performs timer opertations. Use the following commands:
  32. //          TIMER_RESET           - to reset the timer
  33. //          TIMER_START           - to start the timer
  34. //          TIMER_STOP            - to stop (or pause) the timer
  35. //          TIMER_ADVANCE         - to advance the timer by 0.1 seconds
  36. //          TIMER_GETABSOLUTETIME - to get the absolute system time
  37. //          TIMER_GETAPPTIME      - to get the current time
  38. //          TIMER_GETELAPSEDTIME  - to get the time that elapsed between 
  39. //                                  TIMER_GETELAPSEDTIME calls
  40. //-----------------------------------------------------------------------------
  41. enum TIMER_COMMAND { TIMER_RESET, TIMER_START, TIMER_STOP, TIMER_ADVANCE,
  42.                      TIMER_GETABSOLUTETIME, TIMER_GETAPPTIME, TIMER_GETELAPSEDTIME };
  43. FLOAT __stdcall DXUtil_Timer( TIMER_COMMAND command );
  44.  
  45. // E_DIUTILERR_PLAYERWITHOUTDEVICE is returned by the manager class after configuring
  46. // device, and there's a player that hasn't been assigned a device.
  47. #define E_DIUTILERR_PLAYERWITHOUTDEVICE MAKE_HRESULT(SEVERITY_ERROR,FACILITY_ITF,997)
  48.  
  49. // E_DIUTILERR_DEVICESTAKEN is returned by the manager class when one player
  50. // on the machine has enough RECENT devices to prevent other players from 
  51. // playing. This return code is needed because this sample attempts to give 
  52. // all RECENT devices to that player.
  53. #define E_DIUTILERR_DEVICESTAKEN MAKE_HRESULT(SEVERITY_ERROR,FACILITY_ITF,998) 
  54.  
  55. // E_DIUTILERR_TOOMANYUSERS is returned by the manager class when the number of 
  56. // players exceeds the number of devices present on the system. For example, 
  57. // if you ask for 4 players on a machine that only has a keyboard and mouse,
  58. // you're 2 short of what you need. 
  59. #define E_DIUTILERR_TOOMANYUSERS MAKE_HRESULT(SEVERITY_ERROR,FACILITY_ITF,999)
  60.  
  61.  
  62.  
  63. //-----------------------------------------------------------------------------
  64. // Name: class CMultiplayerInputDeviceManager
  65. // Desc: Input device manager using DX8 action mapping
  66. //-----------------------------------------------------------------------------
  67. class CMultiplayerInputDeviceManager
  68. {
  69. public:
  70.     struct PlayerInfo
  71.     {
  72.         DWORD                dwPlayerIndex;         // 0-based player number 
  73.         TCHAR                strPlayerName[MAX_PATH]; // player name
  74.         DWORD                dwMaxDevices;          // max number of elements in pDevices array
  75.         DWORD                dwNumDevices;          // current number of elements in pDevices array
  76.         BOOL                 bFoundDeviceForPlayer; // if a device has been found for this player yet
  77.     };
  78.     
  79.     struct DeviceInfo
  80.     {
  81.         LPDIRECTINPUTDEVICE8  pdidDevice;           // dinput device pointer
  82.         PlayerInfo*           pPlayerInfo;          // Player who owns this device
  83.         BOOL                  bRelativeAxis;        // TRUE if device is using relative axis
  84.         BOOL                  bMapsPri1Actions;     // TRUE if device maps pri 1 actions
  85.         BOOL                  bMapsPri2Actions;     // TRUE if device maps pri 2 actions
  86.         LPVOID                pParam;               // app defined pointer assoicated with this device
  87.         DIDEVICEINSTANCE      didi;                 // device instance info
  88.     };
  89.     
  90.     typedef HRESULT (CALLBACK *LPDIMANAGERCALLBACK)(CMultiplayerInputDeviceManager::PlayerInfo* pPlayerInfo, CMultiplayerInputDeviceManager::DeviceInfo* pDeviceInfo, const DIDEVICEINSTANCE* pdidi, LPVOID);
  91.     
  92. private:
  93.     BOOL                    m_bCleanupCOM;
  94.     HWND                    m_hWnd;
  95.     
  96.     LPDIRECTINPUT8          m_pDI;
  97.     DIACTIONFORMAT*         m_pdiaf;
  98.     
  99.     PlayerInfo**            m_pUsers;
  100.     DWORD                   m_dwNumUsers;    
  101.  
  102.     DeviceInfo*             m_pDeviceList;
  103.     DWORD                   m_dwNumDevices;
  104.     DWORD                   m_dwMaxDevices;    
  105.     
  106.     LPDIMANAGERCALLBACK     m_AddDeviceCallback;
  107.     LPVOID                  m_AddDeviceCallbackParam;
  108.     
  109.     TCHAR*                  m_strKey;
  110.     HKEY                    m_hKey;
  111.     
  112. public:
  113.     static BOOL CALLBACK StaticEnumSuitableDevicesCB( LPCDIDEVICEINSTANCE pdidi, LPDIRECTINPUTDEVICE8 pdidDevice, DWORD dwFlags, DWORD dwRemainingDevices, VOID* pContext );
  114.     static BOOL CALLBACK StaticBuildDeviceListCB( LPCDIDEVICEINSTANCE pdidi, VOID* pContext );
  115.     
  116.     // Device control
  117.     BOOL EnumDevice( const DIDEVICEINSTANCE* pdidi, LPDIRECTINPUTDEVICE8 pdidDevice, DWORD dwFlags, DWORD dwDeviceRemaining );
  118.     BOOL BuildDeviceListCB( LPCDIDEVICEINSTANCE pdidi ); 
  119.     
  120.     HRESULT AddDevice( DeviceInfo* pDeviceInfo, BOOL bForceReset );
  121.     HRESULT GetDevices( DeviceInfo** ppDeviceInfo, DWORD* pdwNumDevices );
  122.     HRESULT ConfigureDevices( HWND hWnd, IUnknown* pSurface, VOID* pCallback, DWORD dwFlags, LPVOID pvCBParam );
  123.     DWORD   GetNumDevices() { return m_dwNumDevices; }
  124.     VOID    UnacquireDevices();
  125.     VOID    SetFocus( HWND hWnd );
  126.     PlayerInfo* LookupPlayer( TCHAR* strPlayerName );
  127.     HRESULT SaveDeviceOwnershipKeys();
  128.     VOID    DeleteDeviceOwnershipKeys();
  129.     HRESULT UpdateDeviceOwnership();
  130.     HRESULT AssignDevices();
  131.     HRESULT VerifyAssignment();
  132.     HRESULT AddAssignedDevices( BOOL bResetMappings );
  133.     
  134.     HRESULT BuildDeviceList();
  135.     VOID    CleanupDeviceList();
  136.     
  137.     // Construction
  138.     HRESULT SetActionFormat( DIACTIONFORMAT* pdiaf, BOOL bReenumerate, BOOL bResetOwnership, BOOL bResetMappings );
  139.     HRESULT Create( HWND hWnd, TCHAR* strUserNames[], DWORD dwNumUsers, DIACTIONFORMAT* pdiaf, LPDIMANAGERCALLBACK AddDeviceCallback, LPVOID pCallbackParam, BOOL bResetOwnership, BOOL bResetMappings );
  140.  
  141.     CMultiplayerInputDeviceManager( TCHAR* strRegKey );
  142.     ~CMultiplayerInputDeviceManager();
  143.     VOID Cleanup();
  144. };
  145.  
  146. #endif
  147.  
  148.  
  149.